﻿using Abp.Web;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Abp.Utils
{
    public class TreeUtils
    {


        /// <summary>
        /// List转换成树
        /// </summary>
        /// <typeparam name="T">泛型参数</typeparam>
        /// <param name="list">集合</param>
        /// <param name="parentId">顶级代码</param>
        /// <param name="where">获取下一级数据过滤器</param>
        /// <param name="action">设置树节点属性</param>
        /// 
        /// 例子：
        ///        return TreeNodeUtils.Convert<Glossary>(glossaries,
        ///                null,
        ///                (glossary, parentId) => {
        ///                    if (string.IsNullOrWhiteSpace(Conv.ToString(parentId)))
        ///                       return string.IsNullOrWhiteSpace(glossary.ParentId);
        ///                    return glossary.ParentId == Conv.ToString(parentId);
        ///                },
        ///                (treeNode, glossary) => {
        ///                    treeNode.Id = glossary.GlossaryId;
        ///                    treeNode.ParentId = glossary.ParentId;
        ///                    treeNode.Text = glossary.Text;
        ///                    treeNode.Checked = false;
        ///                    treeNode.Attributes = new {
        ///                        glossary.Name,
        ///                        glossary.Value
        ///                     };
        ///                });
        /// 
        /// <returns></returns>
        public static List<TreeNode> ConvertToTreeNode<T>(List<T> list,
            object parentId,
            Func<T, object, bool> where,
            Action<TreeNode, T> action)
        {
            List<TreeNode> treeNodes = new List<TreeNode>();
            foreach (var item in list.Where(info => where(info, parentId)))
            {
                TreeNode treeNode = new TreeNode();
                action(treeNode, item);
                if (string.IsNullOrWhiteSpace(Conv.ToString(treeNode.Ancestor)))
                    treeNode.Ancestor = treeNode.Id;
                treeNode.Children = ConvertToTreeNode<T>(list, treeNode.Ancestor, where, action);
                treeNodes.Add(treeNode);
            }
            return treeNodes;
        }


        //public static List<TreeGrid<T>> ConvertToTreeGrid<T,TTreeGrid>(List<T> list,
        //    object parentId,
        //    Func<T, object, bool> where,
        //    Action<TTreeGrid, T> action) where TTreeGrid:TreeGrid<T>,new()
        //{
        //    //List<TTreeGrid> treeNodes = new List<TTreeGrid>();
        //    List<TreeGrid<T>> treeNodes = new List<TreeGrid<T>>();
        //    foreach (var item in list.Where(info => where(info, parentId)))
        //    {
        //        TTreeGrid treeNode = new TTreeGrid();
        //        action(treeNode, item);
        //        treeNode.Children = ConvertToTreeGrid<T,TTreeGrid>(list, treeNode.Id, where, action);
        //        treeNodes.Add(treeNode);
        //    }
        //    return treeNodes;
        //}

    }

}
